// #include "loglevel.hpp"
// #include "format.hpp"
// #include "sink.hpp"
// #include <ctime>
// #include <unistd.h>
// #include "logger.hpp"
// #include <memory>
// #include "buffer.hpp"
// #include "asyncloop.hpp"
// #include <chrono>
// #include <thread>

#include "log.h"
using namespace std;
using namespace ns_logger;
// void test1()
// {
//     cout<<(LogLevel::LevelToString(LogLevel::value::Known))<<endl;
//     cout<<(LogLevel::LevelToString(LogLevel::value::Error))<<endl;
// }
/*
    边缘测试 %g%g%g --不可识别
    % --不可识别
    } 缺少匹配

*/
// 测试消息
//  void test2()
//  {
//      //定义消息类型
//      LogMsg lg(LogLevel::value::Info,"test.cc",13,"root","测试!");
//      Formatter format("[%p][%d{%H:%M:%S}][%c][%t][%f][%l]%T %m%n");
//      std::string msg=format.foramt(lg);
//      cout<<msg;
//  }

// 测试stdout 文件 滚动文件的落地
void test3()
{
    SinkFoctory factory;
    auto file = factory.CreateSink<FileSink>("log/test.txt");
    auto stout = factory.CreateSink<StdoutSink>();
    auto roll = factory.CreateSink<RollSink>("./log/", 1024 * 1024);
    LogMsg lg(LogLevel::value::Info, "test.cc", 31, "root", "落地消息测试!");
    Formatter format;
    std::string msg = format.format(lg);
    stout->log(msg.c_str(), msg.size());
    file->log(msg.c_str(), msg.size());
    int count = 0;
    while (count <= 500000)
    {
        std::string tmp = msg.c_str() + to_string(count);
        roll->log(tmp.c_str(), tmp.size());
        count++;
    }
}

// void test4()
// {
//     //测试以时间落地
//     SinkFoctory factory;
//     auto roll = factory.CreateSink<RollTimeSink>("./date_time/", Gap::GAP_SECOND);
//     LogMsg lg(LogLevel::value::Info, "test.cc", 51, "root", "以时间落地消息落地消息测试!");
//     Formatter format;
//     std::string msg = format.format(lg);
//     int curr_size = 0, i = 0;
//     time_t old = ns_helper::TimeUntil::GetTime();
//     while (old + 5 > time(nullptr))
//     {
//         std::string tmp = msg.c_str() + to_string(i);
//         //  std::this_thread::sleep_for(std::chrono::milliseconds(500));
//         usleep(1000);
//         roll->log(tmp.c_str(), tmp.size());
//         i++;
//         curr_size += msg.size();
//     }
// }

// void test5()
// {
//     SinkFoctory factory;
//     auto file = factory.CreateSink<FileSink>("abc/def/test.txt");
//     auto stout = factory.CreateSink<StdoutSink>();
//     auto roll = factory.CreateSink<RollSink>("./date/", 1024 * 1024);
//     //  格式化
//     FormatterPtr _format(new Formatter);
//     std::vector<LogSinkPtr> sinks{stout, file, roll};
//     LoggerPtr logptr(new SyncLogger("root", LogLevel::value::Warning, _format, sinks));
//     cout << "创建同步日志器" << endl;
//     logptr->Dubug(__FILE__, __LINE__, "输出第1条消息", 1);
//     logptr->Info(__FILE__, __LINE__, "输出第%d条消息", 2);
//     logptr->Warning(__FILE__, __LINE__, "输出第%d条消息", 3);
//     logptr->Error(__FILE__, __LINE__, "输出第%d条消息", 4);
//     logptr->Fatal(__FILE__, __LINE__, "输出第%d条消息", 5);
//     int curr_size = 0, i = 0;
//     std::string msg = "测试日志器功能";
//     while (curr_size <= 1024 * 1024 * 10)
//     {
//         std::string tmp = msg.c_str() + to_string(i);
//         logptr->Fatal(__FILE__, __LINE__, "测试文件写入大小", 5);
//         i++;
//         curr_size += 100;
//     }
// }
// 测试建造者模式
// void test6()
// {
//     shared_ptr<LoggerBuilde> build(new LogcalLoggerBuilde());
//     build->BuildFormat("[%d{%H:%M:%S}]%T:%m");
//     build->BuildName("logger_name");
//     build->BuildType(LogType::SYNC);
//     build->BuildSink<FileSink>("abc/def/test.txt");
//     build->BuildSink<StdoutSink>();
//     build->BuildSink<RollSink>("./date/", 1024 * 1024);
//     LoggerPtr logptr = build->build();
//     logptr->Dubug(__FILE__, __LINE__, "输出第1条消息", 1);
//     logptr->Info(__FILE__, __LINE__, "输出第%d条消息", 2);
//     logptr->Warning(__FILE__, __LINE__, "输出第%d条消息", 3);
//     logptr->Error(__FILE__, __LINE__, "输出第%d条消息", 4);
//     logptr->Fatal(__FILE__, __LINE__, "输出第%d条消息", 5);
//     int curr_size = 0, i = 0;
//     std::string msg = "测试日志器功能";
//     while (curr_size <= 1024 * 1024 * 10)
//     {
//         std::string tmp = msg.c_str() + to_string(i);
//         logptr->Fatal(__FILE__, __LINE__, "测试文件写入大小\n", 5);
//         i++;
//         curr_size += 100;
//     }
// }

// 测试缓冲区
void test7()
{
    // 读文件,读到缓冲区 ，从缓冲区取数据，写到新文件
    ifstream ifs("abc/def/test.txt", std::ios::binary);
    if (!ifs.is_open())
        cout << "打开文件失败" << endl;
    // 获取文件的大小
    ifs.seekg(0, std::ios::end);
    size_t size = ifs.tellg();
    ifs.seekg(0, std::ios::beg);
    cout << "size:" << size << endl;
    // 读文件
    std::string str;
    str.resize(size);
    ifs.read(&str[0], size);
    if (!ifs.good())
        cout << "read fail" << endl;
    ifs.close();
    cout << str.size() << endl;
    // 写入数据
    Buffer buffer;
    for (int i = 0; i < str.size(); i++)
    {
        buffer.Push(&str[i], 1);
    }

    cout << buffer.Readable() << endl;
    // 写文件
    ofstream tmp("abc/def/tmp.txt", std::ios::binary);
    for (int i = 0; i < str.size(); i++)
    {
        tmp.write(buffer.GetFront(), 1);
        buffer.ReadMove(1);
    }
    tmp.close();
}

// void test8()
// {
//     shared_ptr<LoggerBuilde> build(new LogcalLoggerBuilde());
//     build->BuildFormat("[%d{%H:%M:%S}]%T:%m%n");
//     build->BuildName("logger_name");
//     build->BuildType(LogType::ASYNC);
//     build->BuildSink<FileSink>("./log/async.text");
//     // build->EnableUnSafe();
//     build->BuildSink<StdoutSink>();
//     build->BuildSink<RollSink>("./log/test.text", 20);

//     LoggerPtr logptr = build->build();
//     int count = 1;
//     while (count <= 500000)
//     {
//         logptr->Fatal(__FILE__, __LINE__, "测试异步日志%d", count);
//         count++;
//     }
// }
void test9()
{
    // 创建全局日志器
    shared_ptr<LoggerBuilde> builder(new GlobalLoggerBuilder());
    builder->BuildFormat("[%c][%d{%H:%M:%S}]:%m%n");
    builder->BuildName("ansync_logger");
    builder->BuildType(LogType::ASYNC);
    builder->BuildSink<FileSink>("./log/async.text");
    builder->EnableUnSafe();
    builder->BuildSink<StdoutSink>();
    // build->BuildSink<RollSink>("./log/test.text", 20);
    builder->build();
}
// 测试全局日志器管理类
 void test10()
 {
     if (LoggerManager::GetInstance().IsExistsLogger("ansync_logger"))
     {
         cout << "存在" << endl;
     }else{
         cout<<"不存在"<<endl;
     }
     LoggerPtr lg = LoggerManager::GetInstance().GetLogger("ansync_logger");
     int count = 1;
     while (count <= 500000)
     {
         lg->Fatal(__FILE__, __LINE__, "测试全局日志器日志%d", count);
         count++;
     }
 }

// 测试全局接口
void test11()
{
    LoggerPtr lg = getLogger("ansync_logger");
    int count = 1;
    lg->Debug("测试全局日志器日志%d", count++);
    lg->Info("测试全局日志器日志%d", count++);
    lg->Error("测试全局日志器日志%d", count++);
    lg->Warning("测试全局日志器日志%d", count++);
    lg->Fatal("测试全局日志器日志%d", count++);

    while (count <= 20)
    {
        ELOG("测试全局日志器日志%d", count);
        count++;
    }
}

// 测试全局日志器
int main()
{
    // 创建全局日志器
    shared_ptr<LoggerBuilde> builder(new GlobalLoggerBuilder());
    builder->BuildFormat("[%c][%p][%d{%H:%M:%S}]:%m%n");
    builder->BuildName("ansync_logger");
    builder->BuildType(LogType::ASYNC);
    builder->BuildSink<FileSink>("./log/async.text");
    builder->EnableUnSafe();
    builder->BuildSink<StdoutSink>();
    // build->BuildSink<RollSink>("./log/test.text", 20);
    // cout<<"builder构建部件完成"<<endl;
    builder->build();
    test11();
    // test9();
    // test10();
    // test3();
    // test8();
    // test7();
    //  test1();
    //  test2();
    //  test3();
    //  test4();
    //  test6();
    return 0;
}